package com.zhu.util;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * User: karl
 * Date: 16-3-1
 * Time: 下午4:58
 */
public class HttpUtils {
    private static final String POST = "POST";
    private static final String GET = "GET";

    /**
     * Post请求
     * @param url
     * @return
     */
    public static String post(String url) {
        return request(url, POST);
    }

    /**
     * Post请求
     * @param url
     * @param params
     * @return
     */
    public static String post(String url,Map<String, Object> params) {
        return post(url + initParams(params));
    }
    public static String post(String url,JSONObject jsonObj) {
        return post(url + initParams(jsonObj));
    }
    /**
     * Get请求
     * @param url
     * @return
     */
    public static String get(String url) {
        return request(url, GET);
    }

    /**
     * Get请求
     * @param url
     * @param params
     * @return
     */
    public static String get(String url,Map<String, Object> params) {
        return get(url + initParams(params));
    }

    /**
     * 初始化参数
     * @param params
     * @return
     */
    private static String initParams(Map<String, Object> params) {
        StringBuffer paramBuffer = new StringBuffer("?");
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            paramBuffer.append(key).append("=").append(value).append("&");
        }
        paramBuffer = paramBuffer.deleteCharAt(paramBuffer.length() -1);
        return paramBuffer.toString();
    }
    private static String initParams(JSONObject jsonObj) {
        StringBuffer paramBuffer = new StringBuffer("?");
        for (String key : jsonObj.keySet()) {
            String value = jsonObj.getString(key);
            paramBuffer.append(key).append("=").append(value).append("&");
        }
        paramBuffer = paramBuffer.deleteCharAt(paramBuffer.length() -1);
        return paramBuffer.toString();
    }
    /**
     * @param url
     * @param method
     * @return
     */
    private static String request(String url, String method) {
        StringBuffer stringBuffer = new StringBuffer("");
        try {
            URL postUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod(method);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
}
